package com.bawei.module_video.adapter


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_video.R
import com.bawei.module_video.home.model.entity.VideoEntity
import com.bumptech.glide.Glide

class VideoAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val data = arrayListOf<VideoEntity>()
    private var isLoading = false
    
    // 添加点击事件接口
    var onItemClickListener: ((VideoEntity) -> Unit)? = null
    
    // 添加收藏事件接口
    var onFavoriteClickListener: ((VideoEntity, Boolean) -> Unit)? = null

    // 定义两种视图类型
    companion object {
        private const val TYPE_ITEM = 0  // 普通item类型
        private const val TYPE_LOADING = 1  // 加载更多item类型
    }


    fun setData(data: List<VideoEntity>) {
        this.data.clear()
        this.data.addAll(data)

        notifyDataSetChanged()
    }
    // 添加这个方法用于加载更多
    fun addData(newData: List<VideoEntity>) {
        val startPosition = data.size
        data.addAll(newData)
        notifyItemRangeInserted(startPosition, newData.size)
    }

    fun setLoading(isLoading: Boolean) {
        this.isLoading = isLoading
        if (isLoading) {
            notifyItemInserted(data.size)  // 添加加载项
        } else {
            notifyItemRemoved(data.size)  // 移除加载项
        }
    }
    override fun getItemViewType(position: Int): Int {
        return if (position < data.size) TYPE_ITEM else TYPE_LOADING
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_ITEM -> {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_video_video, parent, false)
                VideoViewHolder(view)
            }

            else -> {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_loading_video, parent, false)
                LoadingViewHolder(view)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is VideoViewHolder -> {
                val videoEntity = data[position]
                
                Glide.with(holder.itemView.context)
                    .load(videoEntity.videomainimag)
                    .into(holder.img)
                holder.name.text = videoEntity.name
                holder.introduction.text = videoEntity.title
                
                // 设置收藏按钮状态
                updateFavoriteButton(holder.favoriteButton, videoEntity.isFavorite)
                
                // 设置点击事件
                holder.itemView.setOnClickListener {
                    onItemClickListener?.invoke(videoEntity)
                }
                
                // 设置收藏按钮点击事件
                holder.favoriteButton.setOnClickListener {
                    videoEntity.isFavorite = !videoEntity.isFavorite
                    updateFavoriteButton(holder.favoriteButton, videoEntity.isFavorite)
                    onFavoriteClickListener?.invoke(videoEntity, videoEntity.isFavorite)
                }
            }

            is LoadingViewHolder -> {
                holder.bind(isLoading)  // 更新加载状态
            }
        }
    }
    
    /**
     * 更新收藏按钮状态
     */
    private fun updateFavoriteButton(button: ImageView, isFavorite: Boolean) {
        if (isFavorite) {
            button.setImageResource(R.drawable.ic_star_filled)
            button.setColorFilter(android.graphics.Color.parseColor("#FFC107")) // 黄色
        } else {
            button.setImageResource(R.drawable.ic_star)
            button.setColorFilter(android.graphics.Color.parseColor("#CCCCCC")) // 灰色
        }
    }

    override fun getItemCount() =  data.size + if (isLoading) 1 else 0

    inner class VideoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var img:ImageView
        var name:TextView
        var introduction:TextView
        var favoriteButton: ImageView

        init {
            img = view.findViewById(R.id.img)
            name = view.findViewById(R.id.name)
            introduction = view.findViewById(R.id.introduction)
            favoriteButton = view.findViewById(R.id.favorite_button)
        }
    }
    inner class LoadingViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val progressBar: ProgressBar = view.findViewById(R.id.progressBar)
        val loadingText: TextView = view.findViewById(R.id.loadingText)

        fun bind(isLoading: Boolean) {
            if (isLoading) {
                progressBar.visibility = View.VISIBLE
                loadingText.visibility = View.VISIBLE
            } else {
                progressBar.visibility = View.GONE
                loadingText.visibility = View.GONE
            }
        }
    }
}